package com.sevntu.apvs.duelement;
public class ModelLogicElement {
Element ob ;
	   int [] inSig;
	    public ModelLogicElement(Element ObjectElement, int [] InputSignals)
	    {
	        ob = ObjectElement;
	        inSig = InputSignals;
	    }

   

	   public int [][] OperationModelElement()
	   {
	       int [][] rezMod=new int [3][1];
	       //number of contact
	       String [] mv = (ob.Ncont).split("\\s+");
	       rezMod[0][0]=Integer.parseInt(mv[mv.length-1]);

	       //value of signal

	       rezMod[1][0]=formOutSignal();

	       //time creation of signal

	       String [] s=(ob.time).split("\\s+");
	       int [] time=new int[3];
	       for(int z1=0;z1<3;z1++){time[z1]=Integer.parseInt(s[z1]);}
	       switch(rezMod[1][0])
	       {
	       case 0:rezMod[2][0]=time[0];break;
	       case 1:rezMod[2][0]=time[1];break;
	       case 2:rezMod[2][0]=time[2];break;
	       }


	      return rezMod;
	   }

	   public int formOutSignal()
	   {
	       //defines the value of output signal using truth table
	       int rez=0;
	       
	       String [] mod =(ob.tabI).split("-");
	       for(int i=0;i<mod.length;i++)
	       {
               int nM=0;// number in an array of input signals
	           String [] LogOp=mod[i].split("_");
               int [] rez1= new int[LogOp.length];
	           for(int j=0;j<LogOp.length;j++)
	           {

	              
	        	   int z=0;
	        	   char [] m =LogOp[j].toCharArray();
	        	   while (Character.isDigit(m[z])==true){z++;}
                   if(z==0)
                   { if(inSig.length<10){z=1;}
                   		if(inSig.length>9){z=2;}
                	   LogOp[j]=""+inSig.length+""+LogOp[j];
                	}
	        	   String nn = LogOp[j].substring(0, z);
	        	   int nC=Integer.parseInt(nn); // number of inputs for logic operation
	               String log = LogOp[j].substring(z);   // logic operation
	               int [] vInSig =new int [nC];
	               for(int z2=0;z2<nC;z2++)
                   {
                       vInSig[z2]=inSig[nM+z2];
                   }
	               if(log.equals("AND")==true)
                   {
                       rez1[j]=and(vInSig);
                         nM+=nC;
                   }
	               if(log.equals("OR")==true)
                   {
                       rez1[j]=or(vInSig);
                       nM+=nC;
                   }
	               if(log.equals("NOT")==true)
                   {
                       rez1[j]=not(vInSig);
                       nM+=nC;
                   }
	           }

                inSig=rez1;
	       }
           rez=inSig[0];
	       return rez;
	   }

	   public int and(int [] inS)
	   {
		   int rez=1;
		   int l=inS.length-1;
		   while(l>=0)
		   {
			  
               if((rez==0)&(inS[l]==0)){rez=0;}
               if((rez==0)&(inS[l]==1)){rez=0;}
               if((rez==1)&(inS[l]==0)){rez=0;}
               if((rez==1)&(inS[l]==1)){rez=1;}
               if((rez==0)&(inS[l]==2)){rez=0;}
               if((rez==1)&(inS[l]==2)){rez=2;}
               if((rez==2)&(inS[l]==0)){rez=0;}
               if((rez==2)&(inS[l]==1)){rez=2;}
               if((rez==2)&(inS[l]==2)){rez=2;}
			   l--;
		   }
		   return rez;
	   }
	   public int or(int [] inS)
	   {
		   int rez=0;
		   int l=inS.length-1;
		   while(l>=0)
		   {
			 
               if((rez==0)&(inS[l]==0)){rez=0;}
               if((rez==0)&(inS[l]==1)){rez=1;}
               if((rez==1)&(inS[l]==0)){rez=1;}
               if((rez==1)&(inS[l]==1)){rez=1;}
               if((rez==0)&(inS[l]==2)){rez=2;}
               if((rez==1)&(inS[l]==2)){rez=1;}
               if((rez==2)&(inS[l]==0)){rez=2;}
               if((rez==2)&(inS[l]==1)){rez=1;}
               if((rez==2)&(inS[l]==2)){rez=2;}
			   l--;
		   }
		   return rez;
	   }
	   public int not(int [] inS)
	   {
		   int rez=1;
	
           if(inS[0]==0){rez=1;}
           if(inS[0]==1){rez=0;}
           if(inS[0]==2){rez=2;}
		   return rez;
	   }

}
